package com.brsya.movie.ksAd;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.brsya.movie.R;
import com.brsya.movie.fragment.CategoryFragment;
import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsContentPage;
import com.kwad.sdk.api.KsScene;

/**
 * Fragment展示滑滑流
 */
public class TestContentPageFragment extends TestBaseFragment {
  public static final String KEY_POS_ID = "KEY_POS_ID";
  private long mPosId;
  private TextView mItemType;
  private TextView mItemPosition;
  private TextView mItemPageStatus;
  private TextView mItemVideoStatus;
  protected KsContentPage mKsContentPage;

  public TestContentPageFragment() {}

  public static TestContentPageFragment newInstance(long posId) {
    TestContentPageFragment fragment = new TestContentPageFragment();
    Bundle bundle = new Bundle();
    bundle.putLong(KEY_POS_ID, posId);
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    handleBundle();
  }

  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.test_activity_content_page, container, false);
  }

  @Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    initTestView(view);
    initContentPage();
    initContentPageListener();
    showContentPage();
  }

  private void handleBundle() {
    Bundle bundle = getArguments();
    if (bundle == null) {
      return;
    }
    mPosId = bundle.getLong(KEY_POS_ID);
  }

  /**
   * SDK内需要监听Back事件处理业务，请按照如下方式回调
   */
  @Override
  public boolean onBackPressed() {
    if (mKsContentPage != null && mKsContentPage.onBackPressed()) {
      return true;
    }
    return super.onBackPressed();
  }

  /**
   * 获取KsContentPage
   */
  private void initContentPage() {
    KsScene adScene = new KsScene.Builder(mPosId).build();
    mKsContentPage = KsAdSDK.getLoadManager().loadContentPage(adScene);
  }

  /**
   * 展示KsContentPage
   */
  private void showContentPage() {
    //必须使用getChildFragmentManager
    getChildFragmentManager().beginTransaction()
        .replace(R.id.container, mKsContentPage.getFragment())
        .commitAllowingStateLoss();
  }

  private void initTestView(View rootView) {
    View testLayout = rootView.findViewById(R.id.ksad_test_content_layout);
    boolean isShowVideoPlayEnable = TestContentSetting.getInstance().showVideoPlayEnable();
    testLayout.setVisibility(isShowVideoPlayEnable ? View.VISIBLE : View.GONE);
    View testPauseVideo = rootView.findViewById(R.id.test_pause_video);
    testPauseVideo.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        KsAdSDK.pauseCurrentPlayer();
      }
    });
    View testResumeVideo = rootView.findViewById(R.id.test_resume_video);
    testResumeVideo.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        KsAdSDK.resumeCurrentPlayer();
      }
    });
    mItemType = rootView.findViewById(R.id.item_type);
    mItemPosition = rootView.findViewById(R.id.item_position);
    mItemPageStatus = rootView.findViewById(R.id.item_page_status);
    mItemVideoStatus = rootView.findViewById(R.id.item_video_status);
  }

  protected void initContentPageListener() {
    // 接口回调在主线程，误做耗时操作
    mKsContentPage.setPageListener(new KsContentPage.PageListener() {
      @Override
      public void onPageEnter(KsContentPage.ContentItem item) {
        Log.d("ContentPage", "页面Enter:" + item);
        setFloatingPageStatus(item, "Enter");
      }

      @Override
      public void onPageResume(KsContentPage.ContentItem item) {
        Log.d("ContentPage", "页面Resume:" + item);
        setFloatingPageStatus(item, "Resume");
      }

      @Override
      public void onPagePause(KsContentPage.ContentItem item) {
        Log.d("ContentPage", "页面Pause" + item);
        setFloatingPageStatus(item, "Pause");
      }

      @Override
      public void onPageLeave(KsContentPage.ContentItem item) {
        Log.d("ContentPage", "页面Leave: " + item);
        setFloatingPageStatus(item, "Leave");
      }
    });

    // 接口回调在主线程，误做耗时操作
    mKsContentPage.setVideoListener(new KsContentPage.VideoListener() {
      @Override
      public void onVideoPlayStart(KsContentPage.ContentItem item) {
        Log.d("ContentPage", "视频PlayStart: " + item);
        setFloatingVideoStatus("PlayStart");
      }

      @Override
      public void onVideoPlayPaused(KsContentPage.ContentItem item) {
        Log.d("ContentPage", "视频PlayPaused: " + item);
        setFloatingVideoStatus("PlayPaused");
      }

      @Override
      public void onVideoPlayResume(KsContentPage.ContentItem item) {
        Log.d("ContentPage", "视频PlayResume: " + item);
        setFloatingVideoStatus("PlayResume");
      }

      @Override
      public void onVideoPlayCompleted(KsContentPage.ContentItem item) {
        Log.d("ContentPage", "视频PlayCompleted: " + item);
        setFloatingVideoStatus("PlayCompleted");
      }

      @Override
      public void onVideoPlayError(KsContentPage.ContentItem item, int what, int extra) {
        Log.d("ContentPage", "视频PlayError: " + item);
        setFloatingVideoStatus("PlayError");
      }
    });

    mKsContentPage.setShareListener(new KsContentPage.KsShareListener() {
      @Override
      public void onClickShareButton(String shareData) {
        Log.d("ContentPage",
            "TestContentAllianceActivity onClickShareButton shareData: " + shareData);
        TestMediaShareActivity.launch(getActivity(), CategoryFragment.KS_AD_ID, new KsShareData(shareData));
      }
    });

  }

  @SuppressLint("SetTextI18n")
  private void setFloatingPageStatus(KsContentPage.ContentItem item, String pageStatus) {
    mItemPosition.setText("item position: " + item.position);
    String type;
    if (item.materialType == KsContentPage.ContentItem.MaterialType.CONTENT) {
      type = "content";
    } else if (item.materialType == KsContentPage.ContentItem.MaterialType.AD) {
      type = "ad";
    } else if (item.materialType == KsContentPage.ContentItem.MaterialType.THIRD_AD) {
      type = "third ad";
    } else {
      type = "unknown";
    }
    mItemType.setText("item type: " + type);
    mItemPageStatus.setText("item page: " + pageStatus);
  }


  @SuppressLint("SetTextI18n")
  private void setFloatingVideoStatus(String videoStatus) {
    mItemVideoStatus.setText("item videoStatus: " + videoStatus);
  }
}
